$(function () {
    $("#login").click(function () {
        var options = {
            type: 'POST',
            url: 'Login_action.php',
            data: {
                uname: $("#uname").val(),
                upwd: $("#upwd").val()
            },
            dataType: "json"
        };
        $.ajax(options).done(
            function (result) {
                if (result.returnvalue == "0") {
                    location.href = "Main.php";
                }
                else {
                    $.miniAlert("登录失败！");
                }
            })
        .fail(function () {
            $.miniAlert("异常");
        });
    });

    $("#reg_uname").SetValidateRules({
        required: true,
        //maxlength:10,
        //minlength:5,
        isemail: true
    });
    
//    $("#reg_uname").SetAjaxValidate({
//        url: "CheckUsernameUnique.php",
//        type: "post",
//        success_msg: "",
//        failed_msg: "用户名已经存在",
//        img_ajaxloader_path:"bootstrap/js/MySimpleValidate/ajax-loader.gif"
//    });

    $("#reg_upwd").SetValidateRules({
        required: true,
        maxlength: 20,
        minlength: 6
    //        expression: [{
    //            "exp": /^.*[A-Z].*$/,
    //            "msg": "必须包含一个大写字母"
    //        }, {
    //            "exp": /^.*[!@#$%^&].*$/,
    //            "msg": "必须包含一个非法字符（例如：!@#$%^&）"
    //        }],
        
    });

    $("#reg_confirm_upwd").SetValidateRules({
        required: true,
        sameas: "reg_upwd",
        sameas_msg: "两次输入密码必须相同"
    });

    $("#reg_Captcha").SetAjaxValidate({
        url: "ValidateCaptcha.php",
        type: "post",
        success_msg: "",
        failed_msg: "验证码错误",
        img_ajaxloader_path:"bootstrap/js/MySimpleValidate/ajax-loader.gif"
    });

    $("#register").click(function () {
        var ok = true;
        ok = $("#reg_uname").DoValidate() && ok;
        ok = $("#reg_upwd").DoValidate() && ok;
        ok = $("#reg_confirm_upwd").DoValidate() && ok;

        if (ok) {
            var options = {
                type: 'POST',
                url: 'Register_action.php',
                data: {
                    uname: $("#reg_uname").val(),
                    upwd: $("#reg_upwd").val(),
                    captcha: $("#reg_Captcha").val()
                },
                dataType: "json"
            };
            $.ajax(options).done(
                function (result) {
                    if (result.returnvalue == "0") {
                        $.miniAlert("注册成功！");
                    }
                    else {
                        $.miniAlert("注册失败！" + result.returnvalue);
                    }
                })
            .fail(function () {
                $.miniAlert("异常");
            });
        }
    });

    $("#imgCaptcha").click(function () {
        $("#imgCaptcha").attr("src", "Captcha.php?" + Math.random()+1);
    });

});

